﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Maticsoft.DBUtility;
using System.Data;

namespace CustomsSystem.DAL
{
    public class JobService
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string jobNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Info_job");
            strSql.Append(" where jobNo='" + jobNo + "' ");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(CustomsSystem.Model.Info_job model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if (model.jobNo != null)
            {
                strSql1.Append("jobNo,");
                strSql2.Append("'" + model.jobNo + "',");
            }
            if (model.jobName != null)
            {
                strSql1.Append("jobName,");
                strSql2.Append("'" + model.jobName + "',");
            }
            strSql.Append("insert into Info_job(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CustomsSystem.Model.Info_job model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Info_job set ");
            if (model.jobName != null)
            {
                strSql.Append("jobName='" + model.jobName + "',");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where jobNo='" + model.jobNo + "' ");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string jobNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Info_job ");
            strSql.Append(" where jobNo='" + jobNo + "' ");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }		/// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string jobNolist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Info_job ");
            strSql.Append(" where jobNo in (" + jobNolist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CustomsSystem.Model.Info_job GetModel(string jobNo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" jobNo,jobName ");
            strSql.Append(" from Info_job ");
            strSql.Append(" where jobNo='" + jobNo + "' ");
            CustomsSystem.Model.Info_job model = new CustomsSystem.Model.Info_job();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CustomsSystem.Model.Info_job DataRowToModel(DataRow row)
        {
            CustomsSystem.Model.Info_job model = new CustomsSystem.Model.Info_job();
            if (row != null)
            {
                if (row["jobNo"] != null)
                {
                    model.jobNo = row["jobNo"].ToString();
                }
                if (row["jobName"] != null)
                {
                    model.jobName = row["jobName"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select replicate('--',len(jobno))+ jobName as jobname,jobNo ");
            strSql.Append(" FROM Info_job order by jobno");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetMyList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM Info_job");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" jobNo,jobName ");
            strSql.Append(" FROM Info_job ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Info_job ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        public int ExUser(string jobno)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*) from cu_user");
            strSql.Append(" where jobNo='" +jobno+ "' ");
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
    }
}
